package com.biege.creational.singleton.p04;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * @author: bie
 * @create: 2021-10-04 07:36
 */
public class AppConfig {

    // 饿汉式改造
    private static AppConfig instance = new AppConfig();

    public static AppConfig getInstance() {
        return instance;
    }

    private String parameterA;
    private String parameterB;

    public String getParameterA() {
        return parameterA;
    }

    public String getParameterB() {
        return parameterB;
    }

    private AppConfig() {
        readConfig();
    }

    private void readConfig() {
        Properties prop = new Properties();
        InputStream in = null;
        try {
            // in = AppConfig.class.getResourceAsStream("AppConfig.properties");
            in = new FileInputStream(System.getProperty("user.dir") + "/src/main/java/com/biege/creational/singleton/p01/AppConfig.properties");
            prop.load(in);
            this.parameterA = prop.getProperty("parameterA");
            this.parameterB = prop.getProperty("parameterB");
        } catch (Exception e) {
            System.out.println("装载配置文件出错了，具体堆栈信息如下: ");
            e.printStackTrace();
        } finally {
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}